﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.ServiceEntry;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.Logic;

namespace NTTDATA.BEENChina.ResultCertification.Client.Entry.UI
{
    public partial class EntryHistory : Form
    {
        public EntryHistory()
        {
            InitializeComponent();
            this.Load += new EventHandler(EntryHistory_Load);
        }

        void EntryHistory_Load(object sender, EventArgs e)
        {
            //上次关闭后屏幕位置
            if (LoginHelper.GetInstance().FormEntryListPoint.X != 0 && LoginHelper.GetInstance().FormEntryListPoint.Y != 0)
            {
                this.Location = LoginHelper.GetInstance().FormEntryPoint;
            }
        }

        public void ShowEntryResult(ChildImage imageObject)
        {            
            // imageObject.c
            this.labelFormId.Text = imageObject.FormId;
            this.labelFormName.Text = imageObject.FormName;
            this.labelCategoryName.Text = imageObject.CategoryName;
            this.labelFieldId.Text = imageObject.FieldId;
            this.labelFieldName.Text = imageObject.FieldName;
            clear();
            if (imageObject.OperatorEntryResults.Any(t => t.Rank == EnumRank.A))
            {
                var result = imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.A);
                if (result != null)
                {
                    this.labelCommentA.Text = EntryJob.GetCommnetName(result.EntryCommon);
                    this.labelOpA.Text = result.OperatorId;
                    this.userControlResultA.Entryformat = imageObject.CategorySetting.DataFormat;
                    this.userControlResultA.EntryReult = result.SplitEntryResult;

                    this.userControlResultA.GridItems = imageObject.CategorySetting.CategoryType == EnumCategoryType.选择 ?
                        imageObject.CategorySetting.GridItems : null;

                    this.userControlResultA.Init();
                }
               
            }
           
            if (imageObject.OperatorEntryResults.Any(t => t.Rank == EnumRank.B))
            {
                var result = imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.B);
                if (result != null)
                {
                    this.labelCommentB.Text = EntryJob.GetCommnetName(result.EntryCommon);
                    this.labelOpB.Text = result.OperatorId;
                    this.userControlResultB.Entryformat = imageObject.CategorySetting.DataFormat;
                    //this.userControlResultB.EntryReult = getSplitResult(imageObject.CategorySetting, result.EntryResult);
                    this.userControlResultB.EntryReult = result.SplitEntryResult;

                    this.userControlResultB.GridItems = imageObject.CategorySetting.CategoryType == EnumCategoryType.选择 ?
                        imageObject.CategorySetting.GridItems : null;


                    this.userControlResultB.Init();
                }
            }

            if (imageObject.OperatorEntryResults.Any(t => t.Rank == EnumRank.C))
            {
                var result = imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.C);
                if (result != null)
                {
                    this.labelCommentC.Text = EntryJob.GetCommnetName(result.EntryCommon);
                    this.labelOpC.Text = result.OperatorId;
                    this.userControlResultC.Entryformat = imageObject.CategorySetting.DataFormat;
                    //this.userControlResultC.EntryReult = getSplitResult(imageObject.CategorySetting, result.EntryResult);
                    this.userControlResultC.EntryReult = result.SplitEntryResult;

                    this.userControlResultC.GridItems = imageObject.CategorySetting.CategoryType == EnumCategoryType.选择 ?
                        imageObject.CategorySetting.GridItems : null;

                    this.userControlResultC.Init();
                }
            }

            if (imageObject.OperatorEntryResults.Any(t => t.Rank == EnumRank.D))
            {
                var result = imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.D);
                if (result != null)
                {
                    this.labelCommentD.Text = EntryJob.GetCommnetName(result.EntryCommon);
                    this.labelOpD.Text = result.OperatorId;
                    this.userControlResultD.Entryformat = imageObject.CategorySetting.DataFormat;
                    //this.userControlResultD.EntryReult = getSplitResult(imageObject.CategorySetting, result.EntryResult);
                    this.userControlResultD.EntryReult = result.SplitEntryResult;

                    this.userControlResultD.GridItems = imageObject.CategorySetting.CategoryType == EnumCategoryType.选择 ?
                        imageObject.CategorySetting.GridItems : null;


                    this.userControlResultD.Init();
                }
                

            }
          

            if (imageObject.OperatorEntryResults.Any(t => t.Rank == EnumRank.E))
            {
                var result = imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.E);
                if (result != null)
                {
                    this.labelCommentE.Text = EntryJob.GetCommnetName(result.EntryResult);
                    this.labelOpE.Text = result.OperatorId;
                }
            }

            if (imageObject.OperatorEntryResults.Any(t => t.Rank == EnumRank.Final))
            {
                var result = imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.Final);
                if (result != null)
                {
                    this.labelCommentF.Text = EntryJob.GetCommnetName(result.EntryCommon);
                    this.userControlResultF.Entryformat = imageObject.CategorySetting.DataFormat;
                    //this.userControlResultF.EntryReult = getSplitResult(imageObject.CategorySetting, result.EntryResult);
                    this.userControlResultF.EntryReult = result.SplitEntryResult;

                    this.userControlResultF.GridItems = imageObject.CategorySetting.CategoryType == EnumCategoryType.选择 ?
                        imageObject.CategorySetting.GridItems : null;


                    this.userControlResultF.Init();
                }
            }
            reLayout();
        }

        private void clear()
        {
            clearControlText();

            userControlResultA.EntryReult =
            userControlResultB.EntryReult =
            userControlResultC.EntryReult =
            userControlResultD.EntryReult =
            userControlResultF.EntryReult = null;

            this.userControlResultA.Clear();
            this.userControlResultB.Clear();
            this.userControlResultC.Clear();
            this.userControlResultD.Clear();
            this.userControlResultF.Clear(); 
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private ServiceEntryClient client;

        private void EntryHistory_Resize(object sender, EventArgs e)
        {
            reLayout();
        }

        private void reLayout()
        {

            int maxResultControlWidth = Math.Max(userControlResultA.Width, userControlResultB.Width);
            maxResultControlWidth = Math.Max(maxResultControlWidth, userControlResultC.Width);
            maxResultControlWidth = Math.Max(maxResultControlWidth, userControlResultD.Width);
            maxResultControlWidth = Math.Max(maxResultControlWidth, userControlResultF.Width);

            groupBox2.Top = groupBox1.Bottom;
            groupBox2.Height = panelA.Height + userControlResultA.Height;
            groupBox2.Width = maxResultControlWidth;//userControlResultA.Width;

            groupBox3.Top = groupBox2.Bottom;
            groupBox3.Height = panelB.Height + userControlResultB.Height;
            groupBox3.Width = maxResultControlWidth;//userControlResultB.Width;

            groupBox4.Top = groupBox3.Bottom;
            groupBox4.Height = panelC.Height + userControlResultC.Height;
            groupBox4.Width = maxResultControlWidth;//userControlResultC.Width;

            groupBox5.Top = groupBox4.Bottom;
            groupBox5.Height = panelD.Height + userControlResultD.Height;
            groupBox5.Width = maxResultControlWidth;//userControlResultD.Width;

            groupBox6.Top = groupBox5.Bottom;

            groupBox7.Top = groupBox6.Bottom;
            groupBox7.Height = panelF.Height + userControlResultF.Height;
            groupBox7.Width = maxResultControlWidth;//userControlResultF.Width;
        }


        // 窗体的屏幕坐标
        Point formPoint;
        // 鼠标光标的屏幕坐标
        Point mousePoint;
        private void panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (Control.MouseButtons == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                this.Location = this.formPoint + (Size)mousePos - (Size)this.mousePoint;

                LoginHelper.GetInstance().FormEntryListPoint = this.Location;
            }
        }

        private void panel_MouseDown(object sender, MouseEventArgs e)
        {
            formPoint = this.Location;
            mousePoint = Control.MousePosition;
        }

        private void clearControlText()
        {
            labelCommentA.Text = "";
            labelCommentB.Text = "";
            labelCommentC.Text = "";
            labelCommentD.Text = "";
            labelCommentE.Text = "";
            labelCommentF.Text = "";

            labelWordCountA.Text = "";
            labelWordCountB.Text = "";
            labelWordCountC.Text = "";
            labelWordCountD.Text = "";
            labelWordCountF.Text = "";

            labelOpA.Text = "";
            labelOpB.Text = "";
            labelOpC.Text = "";
            labelOpD.Text = "";
            labelOpE.Text = "";

           /* userControlResultA = new UserControlResult();
            userControlResultB = new UserControlResult();
            userControlResultC = new UserControlResult();
            userControlResultD = new UserControlResult(); 
            
            userControlResultF = new UserControlResult();*/




        }
    }
}